package com.nitty.cash.loop.ui.activity

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import com.nitty.cash.loop.R
import com.nitty.cash.loop.base.app.NittyStackManager
import com.nitty.cash.loop.common.CommonActivity
import com.nitty.cash.loop.databinding.UiLaunchBinding
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.ui.presenter.LaunchPresenter
import com.nitty.cash.loop.utils.NittyImageUtil
import com.nitty.cash.loop.utils.NittyTitleBarUtil
import com.nitty.cash.loop.utils.helper.NittyAnalysisHelper
import com.nitty.cash.loop.utils.helper.TestDataLibInit
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.TimeZone

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/12/5 20:18
 * @Annotation：
 */
class LaunchAct: CommonActivity(),IViewContract.ILaunchView {
    lateinit var binding: UiLaunchBinding
    var presenter: LaunchPresenter? = null

    override fun getNittyRootView(): View? {
        binding = UiLaunchBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initNittyView() {
        NittyTitleBarUtil.setStatusBarLightMode(this, false)
        isInterceptBack(true)
        //关闭所有页面
        NittyStackManager.finishAllActivity(LaunchAct::class.java)
        //启动需要重置审核中、放款中显示的公告提示次数
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_DIALOG_NOTICE_NUM,0)

        val tipsKey = getString(R.string.launch_tips_key)
        val showTips = String.format(getString(R.string.launch_tips),tipsKey)
        val index = showTips.indexOf(tipsKey)
        val spString = SpannableString(showTips)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFD5118")), index, showTips.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textLaunchTips.text = spString

        NittyImageUtil.instance.loadImageGifRes(this,binding.imgLaunchLoading,R.mipmap.gif_launch)
    }

    override fun initNittyListener() {
        binding.textLaunchTips.setOnClickListener {
//            presenter?.getLaunchTimeZoneAction()
        }
    }

    override fun loadNittyData() {
        if (presenter == null){
            presenter = LaunchPresenter(this)
        }
        launch(Dispatchers.Main){
            delay(1000)
            presenter?.getLaunchTimeZoneAction()
        }
    }

    /**
     * 时区转发成功
     */
    override fun showLaunchTimeZoneRequestData(data: String) {
        var timeZone = ""
        var zoneCode = ""
        var zoneCountry = ""

        data.split(",").apply {
            if (this.isNotEmpty()){
                timeZone = this[0].trim()
            }
            if (this.size >= 2){
                zoneCode = this[1].trim()
            }
            if (this.size >= 3){
                zoneCountry = this[2].trim()
            }
        }

        //保存时区id
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_TIMEZONE_ID, TimeZone.getDefault().id)
        //保存转发路径
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_TIMEZONE,timeZone)
        //保存转发地区 区号
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_TIMEZONE_CODE,zoneCode)
        //保存国家标识
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_TIMEZONE_COUNTRY,zoneCountry)

        //初始化analysis lib 赋值 转发地区
        NittyAnalysisHelper.initAnalysisLibAtZoneSuccess()
        //初始化 data lib
        TestDataLibInit.initDataLibAtZoneSuccess()
        //添加埋点
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_LAUNCH_START)

        presenter?.getLaunchConfigsAction()
    }

    /**
     * 进入引导页
     */
    override fun showLaunchGuidePage() {
        onIntent(GuideAct::class.java)
        finish()
    }

    /**
     * 进入权限页
     */
    override fun showLaunchPermissionPage() {
        onIntent(PermissionsAct::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    override fun showLaunchLoginPage() {
        onIntent(LoginAct::class.java)
        finish()
    }

    /**
     * 进入主页
     */
    override fun showLaunchMainPage() {
        onIntent(NittyTabAct::class.java)
        finish()
    }

    override fun requestTryAgain() {
        super.requestTryAgain()
        presenter?.getLaunchTimeZoneAction()
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_LAUNCH_END)
    }
}